** Preparing variables

use "$project_path/data/4_regdata/regdata", clear

// General index excluding one dimention at a time
egen general_no_econ = rowmean(std_diff_m1_hdi std_diff_m1_democracy_index) 
egen general_no_social = rowmean(std_diff_m1_chix_econ std_diff_m1_democracy_index) 
egen general_no_democracy = rowmean(std_diff_m1_chix_econ std_diff_m1_hdi) 

// Only including observations for which all components are available
gen general_all_available = diff_m1_general_chix if diff_m1_democracy_index!=. & diff_m1_chix_econ!=. & diff_m1_hdi!=. 

// Weighted index à la Pocock (1997)
local components = "std_diff_m1_chix_econ std_diff_m1_hdi std_diff_m1_democracy_index"
cor `components' // Correlations of components
matrix Sigma = r(C)
matrix Sigma_inv = inv(Sigma)
matrix input J_tr = (1,1,1)
matrix input J = (1\1\1)
matrix temp = J_tr*Sigma_inv*J // We sum the terms of Sigma_inv
matrix weights = (J_tr*Sigma_inv)/temp[1,1] 
local i = 0
gen temp_numerator = 0
gen temp_denominator =  0
foreach comp in `components' {
	local i = `i'+1
	replace temp_numerator =  temp_numerator + weights[1,`i']*`comp' if `comp'!=.
	replace temp_denominator = temp_denominator + weights[1,`i'] if `comp'!=.
}
gen general_pocock = temp_numerator/temp_denominator
drop temp_denominator temp_numerator

// Taking the average of the components instead of the average of the sub-indices
egen general_components = rowmean(std_diff_m1_growth_gdpc_penn mstd_diff_m1_inflation_imf mstd_diff_m1_unemployment_ilo std_diff_m1_trade_wb std_diff_m1_hdi std_diff_m1_democracy_index)

// Excluding different areas
gen general_no_africa = diff_m1_general_chix if Region!="Africa" 
gen general_no_asia = diff_m1_general_chix if Region!="Asia"
gen general_no_easteur = diff_m1_general_chix if Region!="Eastern Europe" 
gen general_no_latin = diff_m1_general_chix if Region!="Latin America and Caribbean" 
gen general_no_oceania = diff_m1_general_chix if Region!="Oceania" 
gen general_no_west = diff_m1_general_chix if Region!="Western Europe and North America" 

// Excluding different decades
gen decade = Year - mod(Year, 10)
forvalues dec = 1940(10)2010 {
	gen general_no_`dec' = diff_m1_general_chix if decade!=`dec'
}

// Pre/post 1990
gen general_post_1990 = diff_m1_general_chix if Year>1990 
gen general_pre_1990 = diff_m1_general_chix if Year<=1990 

// Excluding small parliaments
gen general_no_small_parl = diff_m1_general_chix if subsample_small_parl!=1 

// Only major elections
gen major_election = 0
replace major_election = 1 if Type_Election=="Presidential" & reg_type=="Presidential" 
replace major_election = 1 if Type_Election=="Parliamentary" & reg_type=="Parliamentary" 
gen general_major_elec = diff_m1_general_chix if major_election==1 

// Without elections coinciding with regime transitions
gen general_no_reg_transition = diff_m1_general_chix if democratization!=1 & dem_reversal!=1 

** Regressions
log using "$project_path/output/appendix_tables/Appendix_Table_D8.smcl", replace

local list_variants = "diff_m1_general_chix general_no_econ general_no_social general_no_democracy general_all_available general_pocock general_components general_no_africa general_no_asia general_no_easteur general_no_latin general_no_oceania general_no_west general_no_1940 general_no_1950 general_no_1960 general_no_1970 general_no_1980 general_no_1990 general_no_2000 general_no_2010 general_no_small_parl general_major_elec general_no_reg_transition"

foreach variant in `list_variants' {
	rdrobust `variant' runvar, all
}

log close

** Building .tex table	  
//Generate a temporary dta file with variables description. We use an id variable that follows the order of the local `variables'.
preserve
gen description = ""

local i = 1
foreach v in `list_variants'{
	
	if "`v'" == "diff_m1_general_chix" local description = "Baseline"
	if "`v'" == "general_no_econ" local description = "Without economic performance"
	if "`v'" == "general_no_social" local description = "Without HDI"
	if "`v'" == "general_no_democracy" local description = "Without democracy"
	if "`v'" == "general_all_available" local description = "All components available"
	if "`v'" == "general_pocock" local description = "Weighted index"
	if "`v'" == "general_components" local description = "Average of components"
	if "`v'" == "general_no_africa" local description = "Without Africa"
	if "`v'" == "general_no_asia" local description = "Without Asia"
	if "`v'" == "general_no_easteur" local description = "Without Eastern Europe"
	if "`v'" == "general_no_latin" local description = "Without Latin America and Caribbean"
	if "`v'" == "general_no_oceania" local description = "Without Oceania"
	if "`v'" == "general_no_west" local description = "Without Western Europe and North America"
	if "`v'" == "general_no_1940" local description = "Without 1940s"
	if "`v'" == "general_no_1950" local description = "Without 1950s"
	if "`v'" == "general_no_1960" local description = "Without 1960s"
	if "`v'" == "general_no_1970" local description = "Without 1970s"
	if "`v'" == "general_no_1980" local description = "Without 1980s"
	if "`v'" == "general_no_1990" local description = "Without 1990s"
	if "`v'" == "general_no_2000" local description = "Without 2000s"
	if "`v'" == "general_no_2010" local description = "Without 2010s"
	if "`v'" == "general_no_small_parl" local description = "Excluding parliamentary elections with $<$ 60 seats"
	if "`v'" == "general_major_elec" local description = "Only major elections of each regime"
	if "`v'" == "general_no_reg_transition" local description = "Without elections coinciding with regime changes"
	
	qui replace description = "`description'" in `i'
	
	local ++i
}
keep description 
drop if description == ""
gen id = _n
tempfile description_dta
save `description_dta'
restore

			  
local rows : word count `list_variants'
//Prepare matrix of coefficients and other statistics
local nrows `rows'
local ncols 4
mat Coefs = J(`nrows', `ncols', .)

local i = 1			  
foreach var in `list_variants'{

	rdrobust `var' runvar, all
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	loca N_`i': di %6.0f e(N)
	
	//Fill the matrix
	mat Coefs[`i',1] = `b_`i''
	mat Coefs[`i',2] = `se_`i''
	mat Coefs[`i',3] = `p_`i''	
	mat Coefs[`i',4] = `N_`i''
	
	local ++i
	
}			  

mat list Coefs

********************************************************************************
* 								EXPORT TABLE
********************************************************************************			
// Clear the dataset, and use the matrix as dataset			

clear
svmat Coefs

rename Coefs1 estimates
rename Coefs2 std_err
rename Coefs3 pval
rename Coefs4 N

gen outcome = ""

gen id = _n
order id outcome, first

tostring estimates, replace force format(%04.3f)

//Add stars
local 3_star 0.01
local 2_star 0.05
local 1_star 0.10

replace estimates = estimates + "***" if pval < `3_star'
replace estimates = estimates + "**" if  pval < `2_star' & pval > `3_star'
replace estimates = estimates + "*" if   pval < `1_star' & pval > `2_star'

foreach var in std_err pval{
	
	tostring `var', replace force format(%04.3f)
	
}
replace std_err = "(" + std_err + ")"
replace pval = "[" + pval + "]"


//Attach source variable and description
merge 1:1 id using `description_dta', assert(match) nogen

replace outcome = description

drop id description

label var outcome "Robustness check"
label var N "N"
label var pval "p-val."
label var std_err "SE"
label var estimates "Est."


local title "General index: Basic robustness checks"
local label_key "tab:robustness_general_index"
local output "$project_path/output/appendix_tables/Appendix_Table_D8.tex"
local footnote = "\emph{Notes}: This table reports estimates for equation (\ref{eq:baseline_rdd}) for variations of the general index. All components available: we only keep observations for which we have data on all sub-indices. Weighted index: we construct the general index using the method of \cite{pocock1997clinical}, giving less weight to sub-indices which are more correlated with each other. Average of components: instead of constructing the general index as the simple average of the sub-indices, we construct it as the simple average of all outcomes which are part of the sub-indices. Only major elections of each regime: we restrict the sample to presidential elections in presidential regimes and parliamentary elections in parliamentary regimes. Without elections coinciding with regime changes: we exclude from the sample elections coinciding with a democratization or a democratic reversal, as measured by \cite{acemoglu2019}."

texsave using "`output'", replace varlabels frag nofix 	location(H) ///
					      align(p{10cm} p{1.7cm} p{1.7cm} p{1.7cm} p{1.7cm}) ///
						  title("`title'")  ///
						  footnote("`footnote'") ///
						  marker("`label_key'") 





	








